
import api from '../data/api'
import err from './error'

var busy = false;
var eventBus = {};
var postForm = {};
var postBank = {
    url: '',
    data: {
        RequestData: '',
        transCode: ''
    }
};
/* var alertData = {
    show:false,
    title:'标题',
    buttontxt:'确定',
    content:'sign'
};
 */

function getForm() {
    for (var a in eventBus.$children) {
        if (eventBus.$children[a].name == 'form')
            postForm = eventBus.$children[a];
    }
}

function init(options) {
    eventBus = options.eventBus;
    err.init({ eventBus: options.eventBus });
}

async function getRedPack(data) {

    if (busy) return;
    busy = true
    var res = await api.invest.redpack(data);
    busy = false;

    if (res.status == true) {

        var checkID = '', reward_money = 0, tender_money = 0;//定义一个空数组
        for (var i = 0; i < res.data.length; i++) {


            if (res.data[i].sed == true) {

                reward_money = reward_money + parseInt(res.data[i].reward_money);
                tender_money = tender_money + parseInt(res.data[i].tender_money);

                if (checkID == "")
                    checkID = res.data[i].id;
                else
                    checkID = checkID + '|' + res.data[i].id;
            }
        }

        eventBus.$store.dispatch('setRedPack', {id:checkID, tender:tender_money, reward:reward_money});
        eventBus.redlist = res.data;
        eventBus.$store.dispatch('setShowDialog', true);
        //eventBus.postBtn.txt = '立即投标', eventBus.postBtn.class = '';
        //eventBus.busy = false, eventBus.show_loading = false;
    //} else {
        //gotend(data);
    }
    return res;
}

async function tender(data) {

    if (busy) return;
    busy = true
    var res = await api.invest.tender(data);


    if (res.status == false) {
        busy = false;
        eventBus.alertData = { show: true, title: '信息提示', buttontxt: '确定', content: res.msg };

        //eventBus.subBtn.txt = '确认投标', eventBus.subBtn.class = 'p_btn';
        //eventBus.postBtn.txt = '立即投标', eventBus.postBtn.class = '';
        //eventBus.busy = false, eventBus.show_loading = false;

        err.revole(res); //处理错误

    } else {

        getForm();
        postForm.show = true
        eventBus.postBank.url = res.action_url
        eventBus.postBank.data.RequestData = res.RequestData
        eventBus.postBank.data.transCode = res.transCode
        postForm.submit();
    }
    return res;
}


export default {
    init,
    tender,
    getRedPack
}
